package mn.slide.dailynews.tv.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import mn.slide.dailynews.R;
import mn.slide.dailynews.model.Tv;
import mn.slide.dailynews.model.TvDayItem;
import mn.slide.dailynews.tv.TvActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class TvListAdapter extends ArrayAdapter<Tv>
{
	private List<Tv>	items;
	private Context		context;
	int					layoutResourceId;

	public TvListAdapter(Context context, int layoutResourceId, List<Tv> items)
	{
		super(context, layoutResourceId, items);
		this.layoutResourceId = layoutResourceId;
		this.items = items;
		this.context = context;
	}

	public View getView(int position, View convertView, ViewGroup parent)
	{
		View row = convertView;
		TvHolder holder = null;
		if ( row == null )
		{
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(layoutResourceId, parent, false);

			holder = new TvHolder();
			holder.tvImage = (ImageView) row.findViewById(R.id.tvImage);
			holder.tvCurrent = (TextView) row.findViewById(R.id.tvCurrent);
			holder.tvCurrentProgress = (ProgressBar) row.findViewById(R.id.tvCurrentProgress);
			holder.tvNext = (TextView) row.findViewById(R.id.tvNext);
			row.setTag(holder);
		}
		else
		{
			holder = (TvHolder) row.getTag();
		}
		Tv tv = getItems().get(position);
		Bitmap bm = BitmapFactory.decodeByteArray(tv.getImage(), 0, tv.getImage().length);// (byte)
																							// олонлогийг
																							// (bitmap)
																							// болгож
																							// байна
		holder.tvImage.setImageBitmap(bm);
		SimpleDateFormat dateFormat = new SimpleDateFormat("H:mm");
		TvDayItem currentTvDayItem = tv.getCurrentTvDayItem();
		if ( currentTvDayItem == null )
			holder.tvCurrent.setText(context.getResources().getString(R.string.tv_no_current));
		else
			holder.tvCurrent.setText(dateFormat.format(currentTvDayItem.getStartDate()) + " " + currentTvDayItem.getName());
		TvDayItem nextTvDayItem = tv.getNextTvDayItem();
		if ( nextTvDayItem == null )
			holder.tvNext.setText(context.getResources().getString(R.string.tv_no_current));
		else
			holder.tvNext.setText(dateFormat.format(nextTvDayItem.getStartDate()) + " " + nextTvDayItem.getName());
		if ( currentTvDayItem != null )
			new CurrentAsyncTask(holder.tvCurrentProgress, currentTvDayItem).execute();
		
		row.setOnClickListener(new View.OnClickListener()
		{
			
			public void onClick(View v)
			{
				Intent intent = new Intent(context, TvActivity.class);
				context.startActivity(intent);
			}
		});
		
		return row;
	}

	@Override
	public int getCount()
	{
		return items.size();
	}

	static class TvHolder
	{
		ImageView	tvImage;
		TextView	tvCurrent;
		ProgressBar	tvCurrentProgress;
		TextView	tvNext;
	}

	public class CurrentAsyncTask extends AsyncTask<Void, Integer, Void>
	{
		int minute = 60 * 1000;
		ProgressBar	progressBar;
		TvDayItem	currentTvItem;
		int			progress;
		int			maxMinutes;
		Date		now;

		public CurrentAsyncTask(ProgressBar progressBar, TvDayItem currentTvItem)
		{
			this.progressBar = progressBar;
			this.currentTvItem = currentTvItem;
			now = new Date();
			maxMinutes = (int) ((currentTvItem.getEndDate().getTime() - currentTvItem.getStartDate().getTime()) / minute);
			progress = (int) ((now.getTime() - currentTvItem.getStartDate().getTime()) / minute);
			progressBar.setMax(maxMinutes);
			progressBar.setProgress(progress);
		}

		@Override
		protected Void doInBackground(Void... params)
		{
			while (progress < maxMinutes + 1)
			{
				publishProgress(progress);
				progress += 1;
				SystemClock.sleep(minute);
			}
			return null;
		}

		@Override
		protected void onProgressUpdate(Integer... values)
		{
			progressBar.setProgress(values[0]);
		}
	}

	public List<Tv> getItems()
	{
		return items;
	}

	public void setItems(List<Tv> items)
	{
		this.items = items;
	}

	public Context getContext()
	{
		return context;
	}

	public void setContext(Context context)
	{
		this.context = context;
	}
}
